package com.xunqi.mall.controller;

import com.github.pagehelper.PageInfo;
import com.xunqi.mall.consts.MallConst;
import com.xunqi.mall.form.ShippingAddForm;
import com.xunqi.mall.service.ShippingService;
import com.xunqi.mall.vo.ResponseVo;
import com.xunqi.mall.vo.UserLoginVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @Description:
 * @Created with IntelliJ IDEA.
 * @author: 夏沫止水
 * @create: 2020-04-24 15:54
 **/
@RestController
public class ShippingController {

    @Resource
    private ShippingService shippingService;

    @PostMapping(value = "/shippings")
    public ResponseVo addShipping(@Valid @RequestBody ShippingAddForm form,
                                  HttpServletRequest request) {

        //获取当前用户登录信息
        UserLoginVo user = (UserLoginVo) request.getSession().getAttribute(MallConst.CURRENT_USER);

        ResponseVo responseVo = shippingService.addShipping(user.getId(), form);

        return responseVo;
    }


    @DeleteMapping(value = "/shippings/{shippingId}")
    public ResponseVo deleteShipping(@PathVariable("shippingId") Integer shippingId,
                                     HttpServletRequest request) {

        //获取当前用户登录信息
        UserLoginVo user = (UserLoginVo) request.getSession().getAttribute(MallConst.CURRENT_USER);

        ResponseVo responseVo = shippingService.deleteShipping(user.getId(), shippingId);

        return responseVo;
    }


    @PutMapping(value = "/shippings/{shippingId}")
    public ResponseVo addShipping(@PathVariable("shippingId") Integer shippingId,
                                  @Valid @RequestBody ShippingAddForm form,
                                  HttpServletRequest request) {

        //获取当前用户登录信息
        UserLoginVo user = (UserLoginVo) request.getSession().getAttribute(MallConst.CURRENT_USER);

        ResponseVo responseVo = shippingService.updateShipping(shippingId,user.getId(), form);

        return responseVo;
    }

    @GetMapping(value = "/shippings")
    public ResponseVo<PageInfo> list(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
                                     HttpServletRequest request) {

        //获取当前用户登录信息
        UserLoginVo user = (UserLoginVo) request.getSession().getAttribute(MallConst.CURRENT_USER);

        ResponseVo<PageInfo> listResponseVo = shippingService.shippingList(user.getId(), pageNum, pageSize);

        return listResponseVo;

    }

}
